package com.javastudy.thread;

import com.javastudy.thread.model.StopThread;

public class StopThreadTest2 {

    public volatile static boolean flag = true;//volatile关键字 保证变量可见，不保证多线程数据一致性，当线程访问变量时缓存中数据会失效，会重新去主存中获取

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            try {
                while (flag){
                    //运行中
                    System.out.println("运行中");
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        Thread.sleep(5000);
        flag = false;
        System.out.println("程序运行结束");
    }
}
